package com.xcap.security.datasourceauthentication.vo;

import com.xcap.security.datasourceauthentication.entity.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class MySecurityUser implements UserDetails {

    private final  SysUser sysUser;

    private List<SimpleGrantedAuthority> authorities;
    public SysUser getSysUser() {
        return sysUser;
    }
    public MySecurityUser(SysUser sysUser) {
        this.sysUser = sysUser;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
       /* GrantedAuthority grantedAuthority=  new GrantedAuthority(){
            @Override
            public String getAuthority() {
                return "student:query";
            }
        };
       List<GrantedAuthority> list= new ArrayList<GrantedAuthority>();
       list.add(grantedAuthority);
        return list; */
        return authorities;
    }

    public void setAuthorities(List<SimpleGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public String getPassword() {
        //密码擦除
        String password = sysUser.getPassword();
        sysUser.setPassword(null);//擦除我们的密码防止传到前端
        return password;
    }

    @Override
    public String getUsername() {
        return this.sysUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return sysUser.getAccountNoExpired().equals(1)?true:false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return sysUser.getAccountNoLocked().equals(1)?true:false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return sysUser.getCredentialsNoExpired().equals(1)?true:false;
    }

    @Override
    public boolean isEnabled() {
        return sysUser.getEnabled().equals(1)?true:false;
    }
}
